package com.dc.project.demo.controller;

import com.dc.base.bo.account.SessionUser;
import com.dc.base.common.aop.AopOperation;
import com.dc.base.common.aop.AopSubmit;
import com.dc.base.common.constants.PermissionTypeConstants;
import com.dc.base.common.controller.BaseController;
import com.dc.base.common.enumeration.MsgOperationTypeEnum;
import com.dc.base.common.enumeration.MsgStatusEnum;
import com.dc.common.util.FileUtil;
import com.dc.common.vo.ResultVO;
import com.dc.project.demo.dto.PmAttributeFormDTO;
import com.dc.project.demo.po.PmAttribute;
import com.dc.project.demo.query.PmAttributeQuery;
import com.dc.project.demo.service.PmAttributeService;
import com.dc.project.demo.vo.PmAttributeVO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 属性管理接口
 *
 * @author Enzo
 * @date 2020-7-8
 */
@Api(tags = "属性管理接口")
@RestController
@RequestMapping("pmAttribute")
public class PmAttributeController extends BaseController {
    private final String MENU = "pmAttribute";
    @Resource
    private PmAttributeService pmAttributeService;

    /**
     * 新增属性
     *
     * @param dto
     * @return com.dc.common.vo.ResultVO
     * @author Enzo
     * @date 2020-10-25
     **/
    @AopSubmit
    @AopOperation(menu = MENU, type = PermissionTypeConstants.ADD)
    @PostMapping("/")
    @ApiOperation("新增属性")
    public ResultVO<PmAttribute> insert(@Valid @RequestBody PmAttributeFormDTO dto) {
        PmAttribute data = pmAttributeService.insertData(dto);
        return success(data, "新增属性成功");
    }

    /**
     * 修改属性信息
     *
     * @param form
     * @return com.dc.common.vo.ResultVO
     * @author Enzo
     * @date 2020-10-25
     **/
    @AopSubmit
    @AopOperation(menu = MENU, type = PermissionTypeConstants.UPDATE)
    @ApiOperation(value = "修改属性信息")
    @PutMapping("/")
    public ResultVO<PmAttribute> updateData(@Valid @RequestBody PmAttributeFormDTO form) {
        PmAttribute data = pmAttributeService.updateData(form);
        return success(data, "修改属性信息成功");
    }

    /**
     * 删除属性
     *
     * @param pk
     * @return com.dc.common.vo.ResultVO
     * @author Enzo
     * @date 2020-10-25
     **/
    @AopOperation(menu = MENU, type = PermissionTypeConstants.DELETE)
    @ApiOperation(value = "删除属性")
    @DeleteMapping("/{pk}")
    public ResultVO deleteData(@PathVariable String pk) {
        pmAttributeService.deleteData(pk);
        return success("删除属性成功");
    }

    /**
     * 分页查询属性信息
     *
     * @param query
     * @return com.dc.common.vo.ResultVO<com.github.pagehelper.PageInfo < com.dc.demo.pm.po.PmAttribute>>
     * @author Enzo
     * @date 2020-10-25
     **/
    @AopOperation(menu = MENU, type = PermissionTypeConstants.LIST)
    @ApiOperation(value = "分页查询属性信息")
    @PostMapping("/page")
    public ResultVO<PageInfo<PmAttribute>> pageData(@RequestBody PmAttributeQuery query) {
        PageInfo<PmAttribute> data = pmAttributeService.pageData(query);
        return success(data, "分页查询属性信息成功");
    }

    @AopOperation(menu = MENU, type = PermissionTypeConstants.LIST)
    @ApiOperation(value = "分页查询我创建的#属性")
    @PostMapping("/pageMyCreate")
    public ResultVO<PageInfo<PmAttribute>> pageMyCreate(@RequestBody PmAttributeQuery query, @ApiIgnore SessionUser sessionUser) {
        query.setCreateUserId(sessionUser.getUserId().toString());
        PageInfo data = pmAttributeService.pageData(query);
        return ResultVO.success(data, "分页查询我创建的记录成功");
    }

    /**
     * 根据主键id查询属性信息
     *
     * @param pk
     * @return com.dc.common.vo.ResultVO<com.dc.demo.pm.po.PmAttribute>
     * @author Enzo
     * @date 2020-10-25
     **/
    @ApiOperation(value = "根据主键id查询属性信息")
    @GetMapping("/{pk}")
    public ResultVO<PmAttribute> getById(@PathVariable String pk) {
        PmAttribute data = pmAttributeService.getById(pk);
        return success(data, "根据主键id查询属性信息成功");
    }

    @AopSubmit
    @AopOperation(menu = MENU, type = PermissionTypeConstants.EXPORT)
    @ApiOperation(value = "导入#属性")
    @PostMapping("/import")
    public ResultVO importData(@RequestParam("file") MultipartFile file) {
        List res = pmAttributeService.importData(file);
        return ResultVO.success(res.size(), "导入#属性成功");
    }

    @AopSubmit
    @AopOperation(menu = MENU, type = PermissionTypeConstants.EXPORT)
    @ApiOperation(value = "导出#属性")
    @PostMapping("/export")
    public ResultVO<String> exportData(@RequestBody PmAttributeQuery query, @ApiIgnore SessionUser sessionUser) {
        getSearchPermission(query, sessionUser);
        String path = pmAttributeService.exportData(query);
        return ResultVO.success(FileUtil.getTempUrl(path), "导出#属性成功");
    }

    @AopOperation(menu = MENU, type = PermissionTypeConstants.LIST, checkPermission = false)
    @ApiOperation(value = "分页查询待我处理记录")
    @PostMapping("/pageMyPending")
    public ResultVO<PageInfo<PmAttributeVO>> pageMyPending(@RequestBody PmAttributeQuery query, @ApiIgnore SessionUser sessionUser) {
        query.setOperationTypeList(new Integer[]{MsgOperationTypeEnum.APPROVE.getCode(), MsgOperationTypeEnum.TRANSACTION.getCode()});
        query.setMsgStatusList(new Integer[]{MsgStatusEnum.UNREAD.getCode(), MsgStatusEnum.READ.getCode()});
        query.setReceiveUserId(sessionUser.getUserId());
        PageInfo<PmAttributeVO> data = pmAttributeService.pageMyMsg(query);
        return ResultVO.success(data, "分页查询待我处理的记录成功");
    }

    @AopOperation(menu = MENU, type = PermissionTypeConstants.LIST, checkPermission = false)
    @ApiOperation(value = "分页查询我已处理记录")
    @PostMapping("/pageMyProcessed")
    public ResultVO<PageInfo<PmAttributeVO>> pageMyProcessed(@RequestBody PmAttributeQuery query, @ApiIgnore SessionUser sessionUser) {
        query.setOperationTypeList(new Integer[]{MsgOperationTypeEnum.APPROVE.getCode(), MsgOperationTypeEnum.TRANSACTION.getCode()});
        query.setMsgStatusList(new Integer[]{MsgStatusEnum.PROCESSED.getCode()});
        query.setReceiveUserId(sessionUser.getUserId());
        PageInfo<PmAttributeVO> data = pmAttributeService.pageMyMsg(query);
        return ResultVO.success(data, "分页查询我已处理的记录成功");
    }

    @AopOperation(menu = MENU, type = PermissionTypeConstants.LIST, checkPermission = false)
    @ApiOperation(value = "分页查询抄送给我的记录")
    @PostMapping("/pageMyCc")
    public ResultVO<PageInfo<PmAttributeVO>> pageMyCc(@RequestBody PmAttributeQuery query, @ApiIgnore SessionUser sessionUser) {
        query.setOperationTypeList(new Integer[]{MsgOperationTypeEnum.NOTICE.getCode()});
        query.setReceiveUserId(sessionUser.getUserId());
        PageInfo<PmAttributeVO> data = pmAttributeService.pageMyMsg(query);
        return ResultVO.success(data, "分页查询抄送给我的记录成功");
    }
}
